package mysqlConnecionMappersPackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dataConnectionPackage.DataConnector;
import dataConnectionPackage.IMapper;
import dataTypesPackage.Liman;

public class LimanMapper implements IMapper {
	private ResultSet rs;
	@Override
	public Object get(int ID) {
		Liman liman;
		// Gets liman with given ID
		rs = DataConnector.getInstance().query("select * from `liman` where `limanID` = " + ID + "");

		if (rs != null){
			try {
				rs.next();
				if (Integer.parseInt(rs.getString(6)) == 1 ){
					 liman= new Liman(Integer.parseInt(rs.getString(1)),rs.getString(2), rs.getString(3),Integer.parseInt(rs.getString(4)),Integer.parseInt(rs.getString(5)));
					 return liman;
				}
				else
					System.out.println("you don't have acces to liman with given ID");
			} catch (NumberFormatException e) {
				System.out.println("limanMapper: number format problem");
				e.printStackTrace();
			} catch (SQLException e) {
				System.out.println("limanMapper: sql problem");
				e.printStackTrace();
			}
		}
		else
			System.out.println("no liman with given ID");
		return null;
	}

	@Override
	public List<Object> getAll() {
		// returns a list<Object> filled with Liman objects from DB
		
		List<Object> allLimanList = new ArrayList<Object>();
		rs = DataConnector.getInstance().query("select * from `liman`;");
		//DataConnector.getInstance().printLastQueryResult();
        try {
        	if (rs != null){
        		while(rs.next()){
        			int numberOfRows = DataConnector.getInstance().countRows("liman");
        			Liman temp;
        			for (int i=1;i<=(numberOfRows*6);i+=6){									//table has 5 coloums. since we add 6 every loop, 6*row needed
        				//					ID,								name,				city,			country,							limanCalisani,		
        				temp = new Liman(Integer.parseInt(rs.getString(i)),rs.getString(i+1), rs.getString(i+2),Integer.parseInt(rs.getString(i+3)),Integer.parseInt(rs.getString(i+4)));
        				allLimanList.add(temp);
        			}               
        		}
        	}
        }
        catch(Exception e){
        	e.printStackTrace();
        }
		return allLimanList;
	}

	@Override
	public int set(Object object) {
		// records given liman, returns ID of it
        boolean isUpdated;
        Liman liman=(Liman) object;
		try {
			/*System.out.println("name:" + liman.getName()  + "     " +
								"city" + liman.getCity()  + "     " +
								"ulke" + liman.getCountry().getUlkeID()  + "     " +
								"calisan" + liman.getLimanCalisaniID());
			 */
			isUpdated = DataConnector.getInstance().update("INSERT INTO `liman` " +
			        "(name, city, country, limanCalisani) " +
			        "values ('"+liman.getName()+"', '"+liman.getCity()+"', '"+liman.getCountry().getUlkeID() +"', '"+liman.getLimanCalisaniID()+"')");
			if (isUpdated)
				return DataConnector.getInstance().countRows("liman");
		} catch (SQLException e) {
			System.out.println("limanMapper: sql problem");
			e.printStackTrace();
		}

		return -1;
	}

}
